var request = undefined;
var equipmentData = [];
var serverDate = undefined;
var menuData = undefined;
var onclickFlag = undefined;
var statuName = undefined;
var statuName1 = undefined;
var level = undefined;
var menuSecond = undefined;
var clickMenuName = undefined;

$(document).ready(function() {
//	showLoading();
	setBaseUrl();
	$("#myMessageModal").load("popup/promptMessage");
	$("#loadingModal").load("popup/loading");
	setTime();
	setInterval(setTime, 1000);
	getMenuData();
	menuData = setInterval(getMenuData, 30000);
	$("#right").addClass("right2");
	$("#iframe").attr("src", "admin/home");
	setTimeout(hideLoginLoad, 2000);
});
function addBottomSolid(ID){
	if (ID) {
		$(".menu").removeClass("bottomSolidGreen");
		$("#" + ID).addClass("bottomSolidGreen");
	}
}
function removeBottomSolid(ID){
	if (ID) {
		$("#" + ID).removeClass("bottomSolidGreen");
	}
}
function dropPage(ID){
	if (ID) {
		$(".menu").removeClass("pageSolidGreen");
		$("#" + ID).addClass("pageSolidGreen");
	}
}
/*以下是老代码*/
function hideLoginLoad() {
	$('#loginLoad').fadeOut("slow", showBody);
}
function showBody() {
	$('#mainBody').show();
}
function showImage(ImageOne, ImageTwo, object, flag, menuID){
	if (!(clickMenuName && object && clickMenuName == object.id)) {
		if (ImageOne) {
			$('#' + ImageOne).show();
		}
		if (ImageTwo) {
			$('#' + ImageTwo).hide();
		}
	}
	$(".middle").hide();
	if (flag) {
		$("#" + menuID).show();
	}
}
function skipMenu(skipUrl, drownDown, ID, linkMenuName, skipID){
	if (clickMenuName && clickMenuName != ID) {
		showImage(clickMenuName + "1", clickMenuName + "2");
	}
	clickMenuName = ID;
	$('.active').removeClass("active");
	$('.activeLi').removeClass("activeLi");
	$(".li3").addClass("activeLi");
	if (ID != '') {
		$("#" + ID).addClass("active");
	}
	if (linkMenuName) {
		clickMenuName = linkMenuName;
		$("#" + linkMenuName).addClass("active");
		showImage(linkMenuName + "2", linkMenuName + "1");
	}
	
	if (menuSecond) {
		$('#' + menuSecond).hide();
	}
	if (drownDown) {
		$('#' + drownDown).show();
	} else {
		showLoading();
		setTimeout(hideLoading, 1500);
	}
	if(skipID && skipID != ''){
		$('.activeLi').removeClass("activeLi");
		$("#"+skipID).addClass("activeLi");
	}
	$("#iframe").attr("src", skipUrl);
	$(".middle").hide();
	if (ID == "homePage") {
		$('.activeLi').removeClass("activeLi");
	}
	menuSecond = drownDown;	
}
function detalisSkip(skipUrl, num, currentPage){
	$("#detalisNum").val(num);
	$("#detalisPage").val(currentPage);
	$("#iframe").attr("src", skipUrl);
	$(".middle").hide();
}
function detalisDrop(skipUrl, num, currentPage, drownDown, object, linkMenuName, skipID){
	$("#detalisNum").val(num);
	$("#detalisPage").val(currentPage);
	if (clickMenuName && clickMenuName != object.id) {
		showImage(clickMenuName + "1", clickMenuName + "2");
	}
	clickMenuName = object && object.id;
	$('.active').removeClass("active");
	$('.activeLi').removeClass("activeLi");
	$(".li3").addClass("activeLi");
	if (object) {
		$(object).addClass("active");
	}
	if (linkMenuName) {
		clickMenuName = linkMenuName;
		$("#" + linkMenuName).addClass("active");
		showImage(linkMenuName + "2", linkMenuName + "1");
	}
	
	if (menuSecond) {
		$('#' + menuSecond).hide();
	}
	if (drownDown) {
		$('#' + drownDown).show();
	} else {
		showLoading();
		setTimeout(hideLoading, 1500);
	}
	if(skipID && skipID != ''){
		$('.activeLi').removeClass("activeLi");
		$("#"+skipID).addClass("activeLi");
	}
	
	menuSecond = drownDown;	
	$("#iframe").attr("src", skipUrl);
	$(".middle").hide();
	
}

function skipSecondMenu(skipUrl, skipID, ID){
	if (skipUrl && skipID) {
		$('.activeLi').removeClass("activeLi");
		$('.active').removeClass("active");
		$("#"+skipID).addClass("activeLi");
		if (clickMenuName && clickMenuName != ID) {
			showImage(clickMenuName + "1", clickMenuName + "2");
		}
		clickMenuName = ID;
		if (ID != '') {
			$("#" + ID).addClass("active");
		}
		$("#iframe").attr("src", skipUrl);
		$(".middle").hide();
	} else {
		showMessge("正在建设中...");
	}
}
function passVal(passValue, passName ,skipUrl, drownDown, object, linkMenuName, skipID){
	 $("#"+ passName).val(passValue); 
	 skipMenu(skipUrl, drownDown, object, linkMenuName, skipID);
}
//确定退出
function sure(data){
	if (onclickFlag) {
		var result = data && data.result;
		if(result == "Success"){
			window.location.href = getRootPath() + "login.jsp";
		} else {
			showMessge("系统异常，请重试");
		}
	} else {
		var contentWindow = $("#iframe") && $("#iframe")[0] && $("#iframe")[0].contentWindow;
		if (contentWindow) {
			contentWindow.sure(data);
		} else {
			showMessge("操作异常");
		}
	}
}

function getUrl(){
	if (onclickFlag) {
		return 'logOut';
	} else {
		var contentWindow = $("#iframe") && $("#iframe")[0] && $("#iframe")[0].contentWindow;
		if (contentWindow && typeof contentWindow.getUrl === 'function') {
			return contentWindow.getUrl();
		} else {
			return undefined;
		}
	}
}

function getData(){
	if (!onclickFlag) {
		var contentWindow = $("#iframe") && $("#iframe")[0] && $("#iframe")[0].contentWindow;
		if (contentWindow && typeof contentWindow.getData === 'function') {
			return contentWindow.getData();
		} else {
			return undefined;
		}
	}
}

function logOut(){
	onclickFlag = "logOut";
	showMessge("确定要退出吗?","model2");
}

function getMenuData() {
	$.ajax({
		type: 'post',
		url: "getMenuData",
		success: function(data) {
			if (typeof data === 'string' && data.indexOf("login") >= 0) {
				loginTimeout();
				return;
			}
			
			if (data) {
				clearInterval(menuData);
//				var userName = data.userName;
//				if (userName) {
//					$("#userName").html(data.userName);
//				} else {
//					$("#userName").html("未知");
//				}
				level = data.level;
				if (level != undefined && level < 4) {
					
				} else {
					
				}
				serverDate = new Date(data.date).getTime();
			} else {
				$("#userName").html("未知");
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$("#userName").html("登录异常");
		},
		timeout: function(data){
			$("#userName").html("登录超时");
		}
	})
}
function setTime() {
	$("#time").html(formatTime(serverDate));
	if (serverDate) {
		serverDate = serverDate + 1000;
	}
	$("#systemTime").val(serverDate);
}
function formatTime(time) {
	var now = new Date();
	if (time) {
		now = new Date(time);
	}
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var day = ((now.getDate() < 10) ? "0" : "") + now.getDate();
	var hours = ((now.getHours() < 10) ? "0" : "") + now.getHours();
	var minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
	var seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
	var week = now.getDay();
	var weekName = [" 星期日 ", " 星期一 ", " 星期二 ", " 星期三 ", " 星期四 ", " 星期五 ", " 星期六 "];
	week = weekName[week];
	now = null;
	return year + "年" + month + "月" + day + "日" + week + hours + minutes + seconds;
}
function loginTimeout() {
	window.location.href = getRootPath() + "login.jsp";
}